﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.Extensions.Configuration;

namespace Config1
{
  public class FxConfigProvider : FileConfigurationProvider
  {
    public FxConfigProvider(FxConfigSource source) : base(source)
    {
     
    }

    public override void Load(Stream stream)
    {
      var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
      XmlDocument xml = new XmlDocument();
      xml.Load(stream);
      var cnnodes = xml.SelectNodes("/configuration/connectionStrings/add");
      foreach (var xmlNode in cnnodes.Cast<XmlNode>())
      {
        var name = xmlNode.Attributes["name"].Value;
        var connectionString = xmlNode.Attributes["connectionString"].Value;

        data[$"{name}:connectionString"] = connectionString;

        var providerName = xmlNode.Attributes["providerName"];
        if (providerName != null)
        {
          data[$"{name}:providerName"]= providerName.Value;
        }
      }


      var setnodes = xml.SelectNodes("/configuration/appSettings/add");
      foreach (var xmlNode in setnodes.Cast<XmlNode>())
      {
        string key = xmlNode.Attributes["key"].Value;
        key = key.Replace(".", ":");
        string value = xmlNode.Attributes["value"].Value;
        data.Add(key, value);
        //data[key] = value;
      }
      this.Data = data;
    }
  }
}
